package com.aries.wallet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.aries.business.ExpenseDataRepository;
import com.aries.business.ExpenseEntity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class ExpenseList extends Activity {
	private ExpenseDataRepository DataRepository = new ExpenseDataRepository((Context)this);
	
	public final static String MESSAGE_DETAIL_ENTITY = "detail_entity";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expense_list);
        
        loadData();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_expense_list, menu);
        return true;
    }
    
    public void addExpense(View view) {
    	Intent intent = new Intent(this, ExpenseEdit.class);
    	
        startActivity(intent);
    }
    
    private void loadData(){
    	DataRepository.Delete();
    	List<ExpenseEntity> expenses = DataRepository.Retrieve();
    	
    	if(expenses.size()<=0){
    		DataRepository.CreateTestData();
    		expenses = DataRepository.Retrieve();
    	}
    	
    	ListView listview = (ListView)findViewById(R.id.activity_expense_list_listview);  
    	    	
    	listview.setDivider(null);
    	listview.setAdapter(new ShiftBackgroundAdapter(this,expenses));     	
    }
    
    public class ShiftBackgroundAdapter extends BaseAdapter{
    	private LayoutInflater Inflater;
    	private List<ExpenseEntity> Expenses;
    	private Context CurrentContext;
    	
    	public final class ViewHolder{
    		public ImageView Icon;
    		public TextView TitleText;
    		public TextView AmoundText;
    		public TextView IDText;    		
    	}

        public ShiftBackgroundAdapter(Context context, List<ExpenseEntity> expenses){
        	this.CurrentContext = context;
            this.Inflater = LayoutInflater.from(context);
            this.Expenses = expenses;
        }
    	
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return this.Expenses.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return this.Expenses.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return Long.valueOf(this.Expenses.get(position).getID()).longValue();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			ExpenseEntity expense = this.Expenses.get(position);
			
			if(convertView == null){
				convertView = Inflater.inflate(R.layout.listview_item, null);
				
				int color = position % 2 == 0? Color.rgb(248, 248, 248): Color.rgb(243, 251, 255);
				convertView.setBackgroundColor(color);
				convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {                       
                        //process
                    	Intent intent = new Intent(CurrentContext, ExpenseDetail.class);
                    	
                    	intent.putExtra(MESSAGE_DETAIL_ENTITY, ((TextView)view.findViewById(R.id.listview_item_id)).getText());    	
                    	
                        startActivity(intent);  
                    }
                });
				
				holder = new ViewHolder();
				
				holder.Icon = (ImageView)convertView.findViewById(R.id.listview_item_image);
				holder.TitleText = (TextView)convertView.findViewById(R.id.listview_item_title);
				holder.AmoundText = (TextView)convertView.findViewById(R.id.listview_item_amount);
				holder.IDText = (TextView)convertView.findViewById(R.id.listview_item_id);				
				
				convertView.setTag(holder);
			}
			else{
				holder = (ViewHolder)convertView.getTag();
			}
			
			holder.Icon.setBackgroundResource(expense.getType() == 0? R.drawable.icon_out : R.drawable.icon_in);
			holder.TitleText.setText(expense.getTitle());
    		holder.AmoundText.setText(String.format("￥.%2f", expense.getAmount()));
    		holder.IDText.setTag(expense.getID());
			
			return convertView;
		}
    	
    }
}
